import 'dart:ui';

import 'package:curved_navigation_bar/curved_navigation_bar.dart';
import 'package:flutter/material.dart';

class AppScrollBehavior extends MaterialScrollBehavior {
  @override
  Set<PointerDeviceKind> get dragDevices => {
        PointerDeviceKind.touch,
        PointerDeviceKind.mouse,
      };
}

class PageViewDemo extends StatefulWidget {
  const PageViewDemo({Key? key}) : super(key: key);

  @override
  State<PageViewDemo> createState() => _PageViewDemoState();
}

class _PageViewDemoState extends State<PageViewDemo> {
  int _currentIndex = 0;
  late PageController _pageController;
  List<Color> colors = [
    Colors.red,
    Colors.orange,
    Colors.yellow,
    Colors.green,
    Colors.teal,
    Colors.blue,
    Colors.purple,
  ];
  List tabbarData = [
    {'label': 'home', 'icon': Icons.home},
    {'label': 'home', 'icon': Icons.home},
    {'label': 'home', 'icon': Icons.home},
    {'label': 'home', 'icon': Icons.home},
    {'label': 'home', 'icon': Icons.home},
    {'label': 'home', 'icon': Icons.home},
    {'label': 'person', 'icon': Icons.person},
  ];

  @override
  void initState() {
    super.initState();
    _pageController = PageController();
  }

  @override
  void dispose() {
    super.dispose();
    _pageController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('PageViewDemo'),
        ),
        body: PageView.builder(
          onPageChanged: ((index) {
            setState(() {
              _currentIndex = index;
            });
          }),
          controller: _pageController,
          scrollBehavior: AppScrollBehavior(),
          itemCount: colors.length,
          itemBuilder: (BuildContext context, int index) {
            return Container(
              color: colors[index],
              child: Center(
                child: Text(
                  '${index + 1}',
                  textScaleFactor: 3,
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 24,
                    color: Colors.black87,
                  ),
                ),
              ),
            );
          },
        ),
        bottomNavigationBar: BottomNavigationBar(
          currentIndex: _currentIndex,
          onTap: ((index) {
            setState(() {
              _currentIndex = index;
            });
            _pageController.animateToPage(index,
                duration: Duration(milliseconds: 300), curve: Curves.ease);
          }),
          items: tabbarData.map((item) {
            var index = tabbarData.indexOf(item);

            return BottomNavigationBarItem(
              label: tabbarData[index]['label'],
              icon: Icon(tabbarData[index]['icon']),
              backgroundColor: Colors.blueGrey,
            );
          }).toList(),
        ),
        // bottomNavigationBar: CurvedNavigationBar(
        //   index: _currentIndex,
        //   backgroundColor: Colors.blueGrey,
        //   items: tabbarData.map((item) {
        //     var index = tabbarData.indexOf(item);
        //     return _currentIndex == index
        //         ? Icon(tabbarData[index]['icon'])
        //         : Column(
        //             children: [
        //               SizedBox(height: 15.0),
        //               Icon(tabbarData[index]['icon']),
        //               Text(
        //                 tabbarData[index]['label'],
        //                 style: TextStyle(color: Colors.black87),
        //               ),
        //             ],
        //           );
        //   }).toList(),
        //   onTap: (index) {
        //     setState(() {
        //       _currentIndex = index;
        //     });
        //     _pageController.animateToPage(index,
        //         duration: Duration(milliseconds: 300), curve: Curves.ease);
        //   },
        // ),
      ),
    );
  }
}
